package com.hjx.windowtool.bk.dibaqu;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Comparator;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableRowSorter;

import com.hjx.windowtool.bk.dibaqu.server.BookChapter;
import com.hjx.windowtool.bk.dibaqu.server.BookInfo;
import com.hjx.windowtool.tablemodel.TableModel;
import com.hjx.windowtool.util.Xiaoshuo2ksDownloadTest2;
import com.hjx.windowtool.util.XiaoshuoDibaquDownloadTest;

import lombok.extern.slf4j.Slf4j;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@Slf4j
public class ChapterListApp {

	private JFrame frame;
	private JTable table;
	private TableModel model;
	private BookInfo book;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					ChapterListApp window = new ChapterListApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public void open(BookInfo book) {
		this.book = book;
		frame.setVisible(true);
		init();
	}
	/**
	 * Create the application.
	 */
	public ChapterListApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JScrollPane scrollPane = new JScrollPane();
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		model = new TableModel("index,title,type,url".split(","), "序号,名称,类型,地址".split(","),BookChapter.class);
		table = new JTable(model);
		TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
		sorter.setComparator(0, new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o1.compareTo(o2);
			}
		});
		table.setRowSorter(sorter);
		table.getColumnModel().getColumn(0).setPreferredWidth(30);
		table.getColumnModel().getColumn(1).setPreferredWidth(150);
		table.getColumnModel().getColumn(2).setPreferredWidth(30);
		table.getColumnModel().getColumn(3).setPreferredWidth(10);
		scrollPane.setViewportView(table);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (table.getSelectedRow()<0) {
					return;
				}
				BookChapter object = (BookChapter) model.getList().get(table.getSelectedRow());
				log.info("{}-{}",object.getTitle(),object.getUrl());
				if(e.getClickCount()==2) {
					try{
						Runtime.getRuntime().exec("cmd /k start "+object.getUrl());
					}catch(Exception ex){}
				}
			}
		});
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		
		JButton button = new JButton("下载");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String path = System.getProperty("user.dir")+"\\book";
				button.setEnabled(false);
				new Thread(new Runnable() {
					public void run() {
						try {
							String s = XiaoshuoDibaquDownloadTest.download(path+"\\"+book.getTitle(), book.getTitle(), book.getUrl());
							Runtime.getRuntime().exec("cmd /k start "+s);
						} catch (Exception e) {
							log.error("下载时发生错误",e);
						}finally {
							button.setEnabled(true);
						}
					}
				}).start();
			}
		});
		panel.add(button);
	}

	private void init() {
		if(book==null) {
			return;
		}
		System.out.println(book.getChapterCount());
		model.setList(book.getChapters());
	}

}
